<?php
use yii\helpers\Html;
use kartik\grid\GridView;
use app\modules\Product\models\Product;
use kartik\datetime\DateTimePicker;
use kartik\date\DatePicker;

$product = Product::findOne($product_id); 
$this->title =  $product->name; 

?>
<style type="text/css">
	table tr td p font{ color:#666;margin-right:10px;}
	.panel .panel-blue .panel-heading{background: #a664a6;color: #FFF;}
</style>

<?php 

$columns[] = [
      'class' => 'kartik\grid\CheckboxColumn',
];

$columns[] = [
	  'class' => 'kartik\grid\ExpandRowColumn',
	  'value' => function ($model, $key, $index, $column) {
		    return GridView::ROW_COLLAPSED;
	   },
	   'detail' => function ($model, $key, $index, $column) {
	   	    $str = '';
	   		if($model->product->stock == 1){ // 国内航线  $m -> Order
				if($model->status <10){	
					$orderRoom = $model->room;
				}else{
					$orderRoom = $model->roomAll;
				}	
				foreach($orderRoom as $room){ // orderRoom
					$str .= "<p>".$room->productRoom->room->name;
					$str .= '  '.$room->productRoom->room->code;
					$str .= '  '.$room->productRoom->people .'人间&nbsp;&nbsp;';
					$customers = $room->orderRoomCustomer;
					if($customers){
						foreach($customers as $c){
							if($c->orderCustomer->status == 1){
							$str .= '/'.$c->orderCustomer->first_name.$c->orderCustomer->last_name.'</a>' ; 
								if($c->orderCustomer->complete == 1){
									$str .= '&nbsp;<i class="fa  fa-check-square"></i>';
								}
							}
						}
					}
					$str .= "</p>";
				}
			}elseif($model->product->stock == 2){ // 海外航线
				$t = json_decode($model->product->rooms, true);	
				if($model->status <10){
					$orderRoom = $model->room;
				}else{
					$orderRoom = $model->roomAll;
				}	
				foreach($orderRoom as $room){ // orderRoom
					$str .= "<p>".$t[$room->room_id]['name'];
					$str .= " &nbsp;&nbsp;&nbsp;";
						$customers = $room->orderRoomCustomer;
						if($customers){
							foreach($customers as $c){
								if($c->orderCustomer->status == 1){
								$str .= ' / '.$c->orderCustomer->first_name.$c->orderCustomer->last_name.'</a>' ; 
								if($c->orderCustomer->complete == 1){
									$str .= '&nbsp;<i class="fa  fa-check-square"></i>';
								}
								}
							}	
						}
					$str .= "</p>";
				}
			}
		
		    return $str;
	    },
];

$columns[] =  [
      'label' => '基本信息',
      'format'=>'raw',
      'value' => function ($model, $index, $widget, $grid)  {
             
            $str = "<p><font>销售</font>".(isset($model->member->id) ? $model->member->profile->name : '(未设置)');
            $str .= "</p><p><font>创建时间</font>".\Yii::$app->formatter->asDate($model->created_at);
            $str .= "</p><p><font>更新时间</font>".\Yii::$app->formatter->asRelativeTime($model->updated_at);
			$str .= "</p><p><font>订单号</font>".$model->code;		
			$str .= "</p>";

            return  $str;
            
      }, 
 ];
 $columns[] =  [
      'label' => '联系人',
      'format'=>'raw',
      'value' => function ($model, $index, $widget, $grid)  {
             
            $str = "<p><font>联系人</font>".$model->username;
            $str .= "</p><p><font>电  话</font>".$model->phone;
            $str .= "</p><p><font>公  司</font>".$model->company;
			$str .= "</p><p><font>备  注</font>".$model->remark;		
			$str .= "</p>";

            return  $str;
            
      }, 
 ];
 $columns[] =  [
      'label' => '金额',
      'format'=>'raw',
      'value' => function ($model, $index, $widget, $grid)  {
             
            $str = "<p><font>舱房金额</font>".Yii::$app->formatter->asCurrency($model->total_sale);
            $str .= "</p><p><font>优惠金额</font>".Yii::$app->formatter->asCurrency($model->sale_price).(($model->check == 1 && $model->sale_price>0) ? '<i class="text-danger fa fa-check-circle" title="优惠审核标识"></i>' : '');
            $str .= "</p><p><font>附加费</font>".Yii::$app->formatter->asCurrency($model->priceSum);
			$str .= "</p><p><font>总金额</font>";
			$colors = ($model->status == 10) ? '#CCC;' : '#FF4500';
			$str .= "<b style='color:".$colors."'>".Yii::$app->formatter->asCurrency($model->price)."</b>";
				
			$str .= "</b></p><p><font>实收</font>".Yii::$app->formatter->asCurrency($model->financeSuccess);
			$str .= "</p>";
            return  $str;
            
      }, 
 ];
 
 $columns[] =  [
      'label' => '状态',
      'format'=>'raw',
      'value' => function ($model, $index, $widget, $grid)  {
             
            $str = "<p>".Yii::$app->params['order_status'][$model->status];
            $str .= "</p><p>".(empty($model->assign_id) ? '未分团' :$model->assign->name) ;
			$str .= "</p><p>".(($model->isRelation > 1) ? '<i class="fa fa-link" title="关联单标识"></i>' : '');	
			$str .= "</p>";
			if($model->is_merge == 2){ 
				$str .="<p><i class='fa fa-handshake-o'".empty($model->merge_id) ? '' : "style=color:red;"."title='拼房标识' ></i></p>";
			};
            return  $str;
            
      }, 
 ];





?>
<div class="order-index">

	
  	
    <h2 class="page-header"><small><?= Html::encode($this->title) ?>&nbsp;<?= $product->go_time ?></small>
        <p class="pull-right">
        	 
        </p>
    </h2>
    
    <?php  echo $this->render('_consearch', ['model' => $searchModel, 'product_id'=>$product_id]); ?>

   	<div style="margin-top: 15px;"></div> 	

	<?= GridView::widget([
	        'id' =>'order-index',
	        'dataProvider' => $dataProvider,
	        'rowOptions' => function ($model, $index, $widget, $grid){
	        },
	        'columns' => $columns,
			'panel' => [
		        'heading'=>'<h3 class="panel-title">订单列表</h3>',
		        'type'=>'blue',
		    ],
		    'toolbar' => [
		        '{toggleData}'
		    ],
		    
	]); ?> 

	<button class="btn btn-success col-md-3"  style="position:fixed;bottom: 6%;right:35%" id="down">下载确认单</button>

	
</div>

<div class="modal fade config-modal"  role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document" style="width:740px;">
      <div class="modal-content">
      	<form id="config-form" method="get" action="/order/word/new-excel" style="margin: 20px;">
	 		<input type="hidden" name="id" id="order_ids" value="">
	 		<input type="hidden" name="product_id" id="product_id" value="<?=$product_id?>">  
	 		<!-- <label></label>
           	<div class="form-group">
           		<、?php 
           		echo '<label>盖章确认截止时间</label>';
				echo DateTimePicker::widget([
					'id' => 'begin_date',
				    'name' => 'begin_date', 
				    'value' => '',
				    'options' => ['placeholder' => '选择日期和时间'],
				    'pluginOptions' => [
				        'format' => 'yyyy-mm-dd hh:ii',
                        'autoclose'=>true,
                        'todayHighlight' => true
				    ]
				]);
				?> 
           	</div>-->
           	<div class="form-group">
           		<?php 
           		echo '<label>付全款截止时间</label>';
				echo DatePicker::widget([
					'id' => 'end_date',
				    'name' => 'end_date', 
				    'value' => '',
				    'options' => ['placeholder' => '选择日期'],
				    'pluginOptions' => [
				        'format' => 'yyyy-mm-dd',
                        'autoclose'=>true,
                        'todayHighlight' => true
				    ]
				]);
				?>
           	</div>
           	<div class="form-group">
           		<?php 
           		echo '<label>模板</label>';
				echo Html::radioList('tem_sheet','2',\yii::$app->params['config_sheet'],['class'=>'form-control']);
				?>
           	</div>
        </form>
        <div class="text-center" style="margin:20px;">
        	<button class="btn btn-success" id="create">生成Excel</button>
       	</div> 	
	  </div>
  </div>
</div>


<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
// 清除模态框缓存
$(".log-modal").on("hidden.bs.modal", function() {
	$(this).removeData("bs.modal");
});


$(".config-modal").on("hidden.bs.modal", function() {
    $(this).removeData("bs.modal");
});

$("#down").click(function(){
	 var keys = $('#order-index').yiiGridView('getSelectedRows');
	
	 if(keys.length == 0){
	 	alert("请选择订单");
	 	return false;
	 }
     $('.config-modal').modal('show');
     $("#order_ids").val(keys);
});

$("#create").click(function(){
	//var begin_date = $("#begin_date").val();
	var end_date = $("#end_date").val();
	if(!end_date){
		alert("时间不能为空！");
		return false;
	}
    $("#config-form").submit();
});
</script>
<?php app\components\JsBlock::end() ?> 
